﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.Student;
using SIMS.Global;

public partial class Student_CreateEdit : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["id"] != null)
            {
                int _StudentId = 0;
                if (int.TryParse(Request.Params["id"].ToString(), out _StudentId) && _StudentId > 0)
                {
                    StudentProfile _profile = new StudentProfile();
                    StudentProfileDB _profileDB = new StudentProfileDB();
                    _profile = _profileDB.GetProfile(_StudentId);

                    if (_profile.Id > 0)
                    {
                        if (_profile.Error.ErrorNo > 0)
                        {
                            lblMessage.Text = _profile.Error.ErrorMessage;
                        }
                        else
                        {
                            hidStudentId.Value = _StudentId.ToString();
                            txtFirstName.Text = _profile.Profile.FirstName;
                            txtLastName.Text = _profile.Profile.LastName;
                            txtDOB.Text = _profile.Profile.DOB.ToString("dd-MMM-yyyy");
                            ddlGender.Text = _profile.Profile.Gender.ToString();
                            txtNRIC.Text = _profile.Profile.NRIC.ToString();
                            txtEmail.Text = _profile.Contact.Email.ToString();
                            ddlContactType.Text = ((int)(_profile.Contact.ContactType)).ToString();
                            txtContactNo.Text = _profile.Contact.ContactNo.ToString();
                            txtExtension.Text = _profile.Contact.Extension.ToString();
                        }
                    }
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                StudentProfile aStudent = new StudentProfile();
                StudentProfileDB _profileDB = new StudentProfileDB();
                int _StudentId = 0;

                if (int.TryParse(hidStudentId.Value, out _StudentId) && _StudentId > 0)
                {
                    aStudent = _profileDB.GetProfile(_StudentId);
                }
                else
                {
                    aStudent.Id = 0;
                    aStudent.Profile = new SIMS.Person.Profile();
                    aStudent.Contact = new SIMS.Person.Contact();
                    aStudent.Error = new MyError();
                }

                if (txtFirstName.Text.Trim().Length > 0)
                {
                    aStudent.Profile.FirstName = txtFirstName.Text;
                }
                else
                {
                    aStudent.Profile.FirstName = string.Empty;
                }

                aStudent.Profile.LastName = txtLastName.Text;

                aStudent.Profile.Gender = Convert.ToChar(ddlGender.Text);

                if (txtNRIC.Text.Trim().Length > 0)
                {
                    aStudent.Profile.NRIC = txtNRIC.Text;
                }
                else
                {
                    aStudent.Profile.NRIC = string.Empty;
                }

                DateTime _dob = DateTime.MinValue;
                if (DateTime.TryParse(txtDOB.Text, out _dob))
                {
                    aStudent.Profile.DOB = _dob;
                }
                else
                {
                    aStudent.Profile.DOB = DateTime.MinValue;
                }

                if (txtEmail.Text.Trim().Length > 0)
                {
                    aStudent.Contact.Email = txtEmail.Text;
                }
                else
                {
                    aStudent.Contact.Email = string.Empty;
                }

                if (ddlContactType.Text != "0")
                {
                    aStudent.Contact.ContactType = (SIMS.Person.ContactType)Enum.Parse(typeof(SIMS.Person.ContactType), ddlContactType.Text);
                }

                if (txtContactNo.Text.Trim().Length > 0)
                {
                    aStudent.Contact.ContactNo = txtContactNo.Text;
                }
                else
                {
                    aStudent.Contact.ContactNo = string.Empty;
                }

                if (txtExtension.Text.Trim().Length > 0)
                {
                    aStudent.Contact.Extension = txtExtension.Text;
                }
                else
                {
                    aStudent.Contact.Extension = string.Empty;
                }

                aStudent.IsActive = false;

                _profileDB.SaveProfile(aStudent);

                if (aStudent.Error.ErrorNo > 0)
                {
                    if (aStudent.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = aStudent.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + aStudent.Error.ErrorLocation + ")";
                        lblMessage.Text += aStudent.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + aStudent.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx");
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Student_CreateEdit");
        }

    }
}